Add version command, closes #201
authorFlorian Hahn <flo@fhahn.com>
Thu, 17 Jul 2014 20:00:58 +0000 (22:00 +0200)
committerFlorian Hahn <flo@fhahn.com>
Fri, 18 Jul 2014 12:42:26 +0000 (14:42 +0200)
Makefile
src/bin/cargo-version.rs [new file with mode: 0644]
src/bin/cargo.rs
tests/test_cargo_version.rs [new file with mode: 0644]
tests/tests.rs

index b638664e4dcd479a8b6bdd7981e188e7c8ec8614..13d5d6f3b33a12c9160c12363204823f87f2092f 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -12,6 +12,16 @@ export LD_LIBRARY_PATH := $(CURDIR)/rustc/lib:$(LD_LIBRARY_PATH)
 export DYLD_LIBRARY_PATH := $(CURDIR)/rustc/lib:$(DYLD_LIBRARY_PATH)
 endif
 
+CFG_RELEASE=0.1.0-pre
+CFG_VER_DATE = $(shell git log -1 --pretty=format:'%ai')
+CFG_VER_HASH = $(shell git rev-parse --short HEAD)
+CFG_VERSION = $(PKG_NAME) $(CFG_RELEASE) ($(CFG_VER_HASH) $(CFG_VER_DATE))
+
+export CFG_RELEASE
+export CFG_VER_DATE
+export CFG_VER_HASH
+export CFG_VERSION
+
 export PATH := $(CURDIR)/rustc/bin:$(PATH)
 
 # Link flags to pull in dependencies
@@ -23,7 +33,8 @@ BINS = cargo \
             cargo-verify-project \
             cargo-git-checkout \
                 cargo-test \
-                cargo-run
+                cargo-run \
+                cargo-version
 
 SRC = $(shell find src -name '*.rs' -not -path 'src/bin*')
 
diff --git a/src/bin/cargo-version.rs b/src/bin/cargo-version.rs
new file mode 100644 (file)
index 0000000..8cadd30
--- /dev/null
@@ -0,0 +1,35 @@
+#![crate_name="cargo-version"]
+#![feature(phase)]
+
+extern crate cargo;
+
+#[phase(plugin, link)]
+extern crate hammer;
+
+#[phase(plugin, link)]
+extern crate log;
+
+extern crate serialize;
+
+use std::os;
+use cargo::execute_main_without_stdin;
+use cargo::core::MultiShell;
+use cargo::util::CliResult;
+
+#[deriving(Decodable,Encodable)]
+pub struct Options;
+
+hammer_config!(Options)
+
+fn main() {
+    execute_main_without_stdin(execute);
+}
+
+fn execute(_: Options, _: &mut MultiShell) -> CliResult<Option<()>> {
+    debug!("executing; cmd=cargo-version; args={}", os::args());
+
+    println!("{}", env!("CFG_VERSION"));
+
+    Ok(None)
+}
index e0590fd4f7b09170772b66c5fc550cc64f631352..f2b7c1c1554d3ddc13d24cf75e43d6fba741341c 100644 (file)
@@ -1,8 +1,11 @@
 #![feature(phase)]
 
 extern crate cargo;
+#[phase(plugin, link)]
 extern crate hammer;
+
 extern crate serialize;
+
 #[phase(plugin, link)]
 extern crate log;
 
@@ -31,7 +34,6 @@ struct ProjectLocation {
 */
 fn execute() {
     debug!("executing; cmd=cargo; args={}", os::args());
-
     let (cmd, args) = process(os::args());
 
     match cmd.as_slice() {
@@ -53,6 +55,7 @@ fn execute() {
             println!("  test           # run the tests");
             println!("  clean          # remove the target directory");
             println!("  run            # build and execute src/main.rs");
+            println!("  version        # displays the version of cargo");
             println!("");
 
 
@@ -60,6 +63,12 @@ fn execute() {
             println!("Options (for all commands):\n\n{}", options);
         },
         _ => {
+            // `cargo --version` and `cargo -v` are aliases for `cargo version`
+            let cmd = if cmd.as_slice() == "--version" || cmd.as_slice() == "-V" {
+                "version".into_string()
+            } else {
+                cmd
+            };
             let command = format!("cargo-{}{}", cmd, os::consts::EXE_SUFFIX);
             let mut command = match os::self_exe_path() {
                 Some(path) => {
diff --git a/tests/test_cargo_version.rs b/tests/test_cargo_version.rs
new file mode 100644 (file)
index 0000000..9fba04a
--- /dev/null
@@ -0,0 +1,12 @@
+use support::{project, execs};
+use hamcrest::assert_that;
+
+fn setup() {}
+
+test!(simple {
+    let p = project("foo");
+
+    assert_that(p.cargo_process("cargo-version"),
+                execs().with_status(0).with_stdout(format!("{}\n",
+        env!("CFG_VERSION")).as_slice()));
+})
index 7db578ec785e0c7ae5a2e3bac2b553ede47bbdcb..0b1852708dd39149148d8a9ee4e9133354812720 100644 (file)
@@ -28,3 +28,4 @@ mod test_cargo_test;
 mod test_shell;
 mod test_cargo_cross_compile;
 mod test_cargo_run;
+mod test_cargo_version;